<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\FangRequest;
use App\Models\City;
use App\Models\Fang;
use App\Models\Fangattr;
use App\Models\Fangowner;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Fang $fang)
    {
        $data = Fang::paginate($this->pagsize);
        return view("admin.fang.index",compact("data","fang"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = Fangattr::all()->toArray();
        $data = subTreeName($data);
//        dump($data);die;
        $ownerData = Fangowner::all();
        $cityData = City::where("pid",1)->get()->toArray();
        return view("admin.fang.create",compact("data","ownerData","cityData"));
    }
    public function area(Request $request){
        $pid = $request->get("pid");
        $data = City::where("pid",$pid)->get();
        return $data;
    }

    # 添加数据处理
    public function store(FangRequest $request)
    {
        $data = $request->except(["file","_token"]);
        Fang::create($data);
        return redirect(route("admin.fang.index"))->with("success","添加成功");
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function show(Fang $fang)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function edit(Fang $fang)
    {
        $attr = Fangattr::get()->toArray();
        $attrData = subTreeName($attr);
        # 读取业主
        $ownerData = Fangowner::all();
        # 读取省份
        $cityData = City::where("pid",1)->get()->toArray();
//        dump($cityData);die;
        $cData = City::where("pid",$fang->fang_province)->get()->toArray();
        $rData = City::where("pid",$fang->fang_city)->get()->toArray();
        return view("admin.fang.edit",compact("fang","attrData","ownerData","cityData","cData","rData"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function update(FangRequest $request, Fang $fang)
    {
        $data = $request->except(["_token","_method","file"]);
        $fang->update($data);
        return redirect( route("admin.fang.index") )->with("success","修改成功");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fang $fang)
    {
        //
    }
}
